package com.example.myapplication.module1.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.viewbinding.ViewBinding
import com.example.myapplication.databinding.ItemContentBinding
import com.example.myapplication.databinding.ItemTitleBinding
import com.example.myapplication.entity.ItemBean
import com.example.mylibrary.adapter.BaseRecyclerAdapter

class CategoryContentAdapter(context: Context) : BaseRecyclerAdapter<ViewBinding>(context) {

    companion object {
        const val TITLE_TYPE = 0
        const val ITEM_TYPE = 1
    }

    var mutableList = mutableListOf<Any>()
        set(value) {
            field = value
            notifyDataSetChanged()
        }

    override fun getItemViewType(position: Int): Int {
        when (mutableList[position]) {
            is String -> {
                return TITLE_TYPE
            }
            is ItemBean -> {
                return ITEM_TYPE
            }
        }
        return super.getItemViewType(position)
    }

    override fun getViewHolder(baseViewHolder: BaseViewHolder) {
//        TODO("Not yet implemented")
    }

    override fun getBinding(
        inflater: LayoutInflater,
        parent: ViewGroup,
        viewType: Int
    ): ViewBinding {
        return when (viewType) {
            TITLE_TYPE -> ItemTitleBinding.inflate(inflater, parent, false)
            else -> ItemContentBinding.inflate(inflater, parent, false)
        }
    }

    override fun bindHolder(holder: BaseViewHolder, position: Int) {
        val content = mutableList[position]
        when (getItemViewType(position)) {
            TITLE_TYPE -> {
                (viewBinding as ItemTitleBinding).tvItemTitle.text = content as String
            }
            ITEM_TYPE -> {
                val itemBean = content as ItemBean
                (viewBinding as ItemContentBinding).tvItemContent.text = itemBean.itemName
                (viewBinding as ItemContentBinding).tvItemContent.setCompoundDrawablesRelativeWithIntrinsicBounds(
                    0,
                    itemBean.itemImage,
                    0,
                    0
                )
            }
        }
    }

    override fun getItemCount(): Int {
        return mutableList.size
    }


}
